package com.example.mywiseelves.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.fonts.Font;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mywiseelves.R;
import com.example.mywiseelves.bean.ElvesBean;
import com.example.mywiseelves.bean.UserBean;
import com.example.mywiseelves.dao.ElvesDao;
import com.example.mywiseelves.dao.UserDao;
import com.example.mywiseelves.service.ElvesViewService;
import com.example.mywiseelves.utils.AccessibilityUtil;
import com.google.android.material.appbar.CollapsingToolbarLayout;

import java.util.Random;

public class IllustrateDetailsActivity extends AppCompatActivity implements View.OnClickListener {
    //返回
    ImageView illustrateDetailsImageView;
    //标题栏
    CollapsingToolbarLayout collapsingToolbar;
    ImageView illustrateDetailsElvesIv;
    TextView illustrateDetailsTv;
    AnimationDrawable animationDrawable;
    //赋予生命
    Button getLifeButton;
    //复原生命
    Button cancelLifeButton;
    TextView advertisementTv;
    ElvesBean elvesBean;
    //用户dao对象
    UserDao userDao;
    ElvesDao elvesDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_illustrate_details);
        userDao=new UserDao(this);
        elvesDao = new ElvesDao(this);
        elvesBean = (ElvesBean) getIntent().getSerializableExtra("elves_data");
        findViews();
        initViews();
        initListener();
        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(getApplicationContext())) {
                //启动Activity让用户授权
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                startActivity(intent);
                return;
            }
        }
    }

    /**
     * 实例化控件
     */
    private void findViews() {
        getLifeButton = findViewById(R.id.getLife_bt);
        cancelLifeButton = findViewById(R.id.cancelLife_bt);
        advertisementTv = findViewById(R.id.advertisement_tv);
        collapsingToolbar = findViewById(R.id.illustrateDetailsCollapsing_toolbar);
        illustrateDetailsImageView = findViewById(R.id.illustrateDetails_back_iv);
        illustrateDetailsElvesIv = findViewById(R.id.illustrateDetails_elves_iv);
        illustrateDetailsTv = findViewById(R.id.illustrateDetails_tv);
    }

    /**
     * 初始化控件
     */
    public void initViews() {
        collapsingToolbar.setExpandedTitleTypeface(Typeface.MONOSPACE);
        collapsingToolbar.setCollapsedTitleTypeface(Typeface.DEFAULT);
        collapsingToolbar.setExpandedTitleColor(getResources().getColor(R.color.title_color));
        collapsingToolbar.setCollapsedTitleTextColor(getResources().getColor(R.color.white));
        collapsingToolbar.setTitle(elvesBean.getElves_name());
        illustrateDetailsTv.setText(elvesBean.getElves_message());
        illustrateDetailsElvesIv.setBackgroundResource(elvesBean.getElves_anim_normal());
        animationDrawable = (AnimationDrawable) illustrateDetailsElvesIv.getBackground();
        animationDrawable.start();
        if (elvesBean.getElves_start() == 0) {
            getLifeButton.setVisibility(View.VISIBLE);
            cancelLifeButton.setVisibility(View.GONE);
        } else if (elvesBean.getElves_start() == 1) {
            getLifeButton.setVisibility(View.GONE);
            cancelLifeButton.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 实例化监听
     */
    public void initListener() {
        illustrateDetailsImageView.setOnClickListener(this);
        getLifeButton.setOnClickListener(this);
        cancelLifeButton.setOnClickListener(this);
        advertisementTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.getLife_bt) {
//            checkAccessibility();
            getLifeButton.setVisibility(View.GONE);
            cancelLifeButton.setVisibility(View.VISIBLE);
            elvesDao.allElvesStartUpdate();//归零
            elvesBean.setElves_start(1);
            elvesDao.elvesStartUpdate(elvesBean);
            Intent intent = new Intent(getApplicationContext(), ElvesViewService.class);
            stopService(intent);
            intent.putExtra("elves_data", elvesBean);
            startService(intent);
            Toast.makeText(this,"出来吧！"+elvesBean.getElves_name(), Toast.LENGTH_LONG).show();
        } else if (v.getId() == R.id.cancelLife_bt) {
            getLifeButton.setVisibility(View.VISIBLE);
            cancelLifeButton.setVisibility(View.GONE);
            elvesBean.setElves_start(0);
            elvesDao.elvesStartUpdate(elvesBean);
            Intent intent = new Intent(getApplicationContext(), ElvesViewService.class);
            stopService(intent);
        } else if (v.getId() == R.id.advertisement_tv) {
            UserBean userBean=userDao.userQuery();
            int user_point=userBean.getUser_point();
            int user_ad = userBean.getUser_ad();
            if (userBean.getUser_ad()<3){
                //判断广告状态
                Random r = new Random();
                int i1 = r.nextInt(999 - 99) + 99;
                userBean.setUser_point(user_point+i1);
                userBean.setUser_ad(user_ad+1);
                userDao.userPointUpdate2(userBean);
                Toast.makeText(this,userBean.getUser_name()+",观看成功，获得："+i1+"祈愿点"+",当前观看次数："+userBean.getUser_ad()+"次", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(this,"今日已到达观看最大次数,当前观看次数："+userBean.getUser_ad()+"次", Toast.LENGTH_LONG).show();
            }
        } else if (v.getId() == R.id.illustrateDetails_back_iv) {
            finish();
        }
    }
}